package com.hunan.api.catv.service.biz.system;

import com.google.gson.JsonObject;
import com.hunan.api.catv.common.CacheKey;
import com.hunan.api.catv.common.CacheName;
import com.hunan.api.catv.entity.ServiceResult;
import com.hunan.api.catv.entity.enums.RefreshCacheEnum;
import com.hunan.api.catv.entity.po.SystemUser;
import com.hunan.api.catv.entity.vo.SystemRoleVo;
import com.hunan.api.catv.entity.vo.SystemUserVo;
import com.hunan.api.catv.mq.MqConstants;
import com.hunan.api.catv.service.ResultService;
import com.hunan.api.catv.service.biz.cache.CacheBiz;
import com.hunan.api.catv.service.biz.mq.MqBiz;
import com.hunan.api.catv.service.impl.system.SystemUserService;
import com.hunan.api.catv.utils.RedisUtils;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.ObjectUtils;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

@CacheConfig(cacheNames = "SystemBiz")
@Service
public class SystemBiz extends ResultService {


    @Autowired
    SystemUserService systemUserService;

    @Autowired
    RedisUtils redisUtils;

    @Autowired
    CacheBiz cacheBiz;

    @Autowired
    SystemRoleBiz systemRoleBiz;

    @Autowired
    MqBiz mqBiz;

    /**
     * admin 登录
     *
     * @param userMail
     * @param password
     * @return
     */
    public ServiceResult login(String userMail, String password, String ip) {
        SystemUser systemUser = new SystemUser();
        systemUser.setSysUserMail(userMail);
        SystemUserVo systemUserVo = systemUserService.get(systemUser);
        if (ObjectUtils.isEmpty(systemUserVo)) {
            return buildFail("", "用户不存在");
        }
        SystemRoleVo systemRoleVo = systemRoleBiz.info(systemUserVo.getSysRoleId());
        if (systemUserVo.getState().equals(1) && systemRoleVo.getState().equals((byte) 1)) {
            String md5pwd = DigestUtils.md5DigestAsHex(password.getBytes());
            if (systemUserVo.getSysUserPwd().equals(md5pwd)) {
                String uuid = systemUserVo.getSysUserMail() + DateTime.now().getMillis();
                String token = DigestUtils.md5DigestAsHex(uuid.getBytes());
                String token_key = MessageFormat.format(CacheKey.adminToken, token);
                systemUserVo.setLoginIp(ip);
                redisUtils.set(token_key, systemUserVo, 604800L);
                JsonObject result = new JsonObject();
                result.addProperty("token", token);
                result.addProperty("userName", systemUserVo.getSysUserName());
                result.addProperty("userMail", systemUserVo.getSysUserMail());
                return buildOK(result, "登录成功");
            }
        }
        return buildFail("", "用户名或密码错误");
    }

    public ServiceResult logout(String token) {
        String token_key = MessageFormat.format(CacheKey.adminToken, token);
        redisUtils.del(token_key);
        return buildOK("", "logout success");
    }

    public ServiceResult deleteBizCache(String state) {
        List<String> keys = new ArrayList<>();
        if (!"0614".equals(state)) {
            return buildFail(false, "parameter error");
        }
        keys.add(CacheName.live);
        keys.add(CacheName.upgrade);
        keys.add(CacheName.activity);
        keys.add(CacheName.album);
        keys.add(CacheName.news);
        keys.add(CacheName.recommend);
        keys.add(CacheName.column);
        keys.add(CacheName.tag);
        keys.add(CacheName.system);
        String[] key = new String[keys.size()];
        redisUtils.delObjectCacheList(keys.toArray(key));
        return buildOK(true, "");
    }

    public ServiceResult refreshCache(Integer code) {
        RefreshCacheEnum refreshCacheEnum = RefreshCacheEnum.getResult(code);
        if (ObjectUtils.isEmpty(refreshCacheEnum)) {
            return buildOK("", "-1");
        }
        return cacheBiz.flushCache(refreshCacheEnum.getLocal(), refreshCacheEnum.getRedis().split(","));
    }

    public ServiceResult refreshInfoRedisCache(String pwd, String key) {
        if (!"c8837b23ff8aaa8a2dde915473ce0991".equals(pwd) || ObjectUtils.isEmpty(key)) {
            return buildFail("错误");
        }
        String[] keys = key.split(",");
        for (int i = 0; i < keys.length; i++) {
            String k = keys[i];
            redisUtils.delObjectCacheList(k);
        }
        return buildOK(true);
    }

    public ServiceResult refreshInfoLocalCache(String pwd, String key) {
        if (!"c8837b23ff8aaa8a2dde915473ce0991".equals(pwd) || ObjectUtils.isEmpty(key)) {
            return buildFail("错误");
        }
        String[] keys = key.split(",");
        for (int i = 0; i < keys.length; i++) {
            String k = keys[i];
            mqBiz.pushFANOUT(MqConstants.local_cache_exchange, k);
//			@SuppressWarnings("unchecked")
//			LoadingCache<Object,?> bean = SpringBeanUtils.getBean(k, LoadingCache.class);
//			bean.invalidateAll();
        }
        return buildOK(true);
    }
}
